import itertools

with open('input.txt') as file:
    input = file.read()

    grid = set()
    mins = [-1, -1, -1]
    maxs = [1, 1, 1]

    def neighbors(cord):
        count = 0
        for d in [(x, y, z) for x in (-1,0,1) for y in (-1,0,1) for z in (-1,0,1)]:
            ncord = tuple(c + dc for c, dc in zip(cord, d))
            if cord == ncord:
                continue
            if ncord in grid:
                count += 1
                if count > 3:
                    return count
        return count

    # init state
    for y, line in enumerate(input.splitlines()):
        for x, c in enumerate(line):
            if c == '#':
                grid.add((x, y, 0))

    maxs[0] = maxs[1] = y + 1

    for _ in range(6):
        new_grid = set()

        for cord in itertools.product(
            *(range(lo, hi + 1) for (lo, hi) in zip(mins, maxs))
        ):
            n = neighbors(cord)
            if cord in grid:
                if n in (2, 3):
                    new_grid.add(cord)
            elif n == 3:
                new_grid.add(cord)
        
        grid = new_grid
        mins = [m - 1 for m in mins]
        maxs = [m + 1 for m in maxs]
    
    print(len(grid))